
const express = require('express')
const path = require('path')


const multer = require("multer");

const app = express()

// 开启静态资源托管
app.use("", express.static("./uploads"));

const port = 8000
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads/')
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);

    cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
  }
})

// 指定文件存放在哪里！！ 
const upload = multer({ storage: storage })

app.post('/uploadFile', upload.single('file_data'), function (req, res) {
  // 上传的文件对象
  console.log(req.file);
  // http://127.0.0.1:8000/file_data-1636963645141-900008095.jpg
  res.send(`http://127.0.0.1:8000/${req.file.filename}`);
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

